/**
 * FdocForm =  class
 *
 * @author Dang Thanh 2010/06/14.
 */
var Common =
{

    /**
     *
     */
    jsonToForm : function(jsonObject)
    {
        for (var property in jsonObject) {
            if (jsonObject.hasOwnProperty(property)) {
                try{
                    this.setValueByName(property, jsonObject[property]);
                }
                catch(e){
                //alert(e.message);
                }
            }
        }
    },

    /**
     *
     */
    formToJson : function(formName)
    {
        var form = document.getElementsByName(formName)[0];
        var valueForm = {};

        for (var i=0; i < form.elements.length;i++) {
            try{
                valueForm[form.elements[i].name]  = this.getValueByName(form.elements[i].name);
            }
            catch(e){

            }
        }

        return YAHOO.lang.JSON.stringify(valueForm);
    },

    getControlByName : function(inputName){
        var elements = document.getElementsByName(inputName);
        return elements[0];
    },

    getValueByName : function(inputName){
        var elements = document.getElementsByName(inputName);

        // Fix for lable, div
        if(elements.length == 0){
            var element = document.getElementById(inputName);
            return element.innerHTML;
        }
        
        var valueArray = new Array();
        var type;

        for(i=0;i<elements.length;i++){
            type = elements[i].type.toLowerCase();
            if(type == 'text' || type == 'select-one' || type == 'textarea' || type == 'hidden' ){
                valueArray.push(elements[i].value);
            }
            else if(type == 'select-multiple'){
                valueArray.push(elements[i].value);
            }
            else if(type == 'radio' || type == 'checkbox'){
                if(elements[i].checked){
                    valueArray.push(elements[i].value);
                }
            }
            else{
                valueArray.push(elements[i].value);
            }
        }

        if(elements.length == 0){
            //return undefined;
            return '';
        }
        else if(elements.length == 1) {
            return valueArray.join(',');
        }
        else{
            return valueArray;
        }
    },

    /**
     *
     */
    setValueByName : function(inputName,value){
        var elements = document.getElementsByName(inputName);
    
        // Fix for firefox
        if(elements.length == 0){
            var element = document.getElementById(inputName);
            element.innerHTML = value.toString();
            return;
        }

        // For IE
        var type;
        for(i=0;i<elements.length;i++){

            if ( elements[i].type == undefined ){
                elements[i].innerHTML = value.toString();
            }
            else{
                type = elements[i].type.toLowerCase();
                if(type == 'text'  || type == 'textarea' || type == 'hidden' ){
                    elements[i].value = value.toString();
                }
                else if(type == 'select-multiple' || type == 'select-one' ){
                    this.setSelectValue(elements[i], value);
                }
                else if(type == 'checkbox'){
                    this.setCheckboxValue(elements[i], value);
                }
                else if(type == 'radio'){
                    this.setRadioValue(elements[i], value);
                }
                else{
                    elements[i].value = value.toString();
                }
            }
        }
    },

    setSelectValue : function (element,value) {
        var aryValue = value.toString().split(",");

        var tmp ;
        if( element.options[0] != null) element.options[0].selected = true;

        for(var i=0;i< aryValue.length;i++){
            tmp = aryValue[i];
            for (var j = 0; j < element.length; j++) {
                element.options[j].selected = false;
                if(tmp.toString() == element.options[j].value){
                    element.options[j].selected = true;
                }
            }
        }
    },

    getSelectText : function (inputName) {
        var elements = document.getElementsByName(inputName);
        var element ;
        var text = '';
        // Fix for firefox
        if(elements.length == 0){
            element = document.getElementById(inputName);
        }
        else{
            element = elements[0];
        }
        
        for (var j = 0; j < element.length; j++) {
            
            if(element.options[j].selected == true){
                text =  element.options[j].text;
                break;
            }
        }

        return text;
    },
    
    setRadioValue : function (element, value) {

        var radioLength = element.length;
        if(radioLength == undefined) {
            element.checked = (element.value == value.toString());
            return;
        }
        for(var i = 0; i < radioLength; i++) {
            element[i].checked = false;
            if(element[i].value == value.toString()) {
                element[i].checked = true;
            }
        }
    },

    setCheckboxValue : function (element,value) {
        element.checked = false;
        var aryValue = value.toString().split(",");
        var tmp ;

        if(aryValue.length < 1){
            if(value.toString() == element.value){
                element.checked = true;
            }
        }
        else{
            for(var i=0;i< aryValue.length;i++){
                tmp = aryValue[i];
                if(tmp.toString() == element.value){
                    element.checked = true;
                }
            }
        }
    },
    setSelectText : function (inputName,textValue) {
        var elements = document.getElementsByName(inputName);
        var element ;
        //set selected by text
        // Fix for firefox
        if(elements.length == 0){
            element = document.getElementById(inputName);
        }
        else{
            element = elements[0];
        }

        for (var j = 0; j < element.length; j++) {

            if(element.options[j].text == textValue){
                //set selected
                element.options[j].selected = true;
                break;
            }
        }
        
    },

    isExplorer : function(){
        if (navigator.appName.indexOf('Microsoft')!=-1)
            return true;
        else
            return false;
    },

    /*
        PURPOSE: Remove leading blanks from our string.
        IN: str - the string we want to LTrim
     */
    LTrim:function(str){
        
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1) {
            // We have a string with leading blank(s)...

            var j=0, i = s.length;

            // Iterate from the far left of string until we
            // don't have any more whitespace...
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;

            // Get the substring from the first non-whitespace
            // character to the end of the string...
            s = s.substring(j, i);
        }
        return s;
    },
    /*
    PURPOSE: Remove trailing blanks from our string.
    IN: str - the string we want to RTrim
     */
    RTrim:function(str){
        // We don't want to trip JUST spaces, but also tabs,
        // line feeds, etc.  Add anything else you want to
        // "trim" here in Whitespace
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            // We have a string with trailing blank(s)...

            var i = s.length - 1;       // Get length of string

            // Iterate from the far right of string until we
            // don't have any more whitespace...
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;


            // Get the substring from the front of the string to
            // where the last non-whitespace character is...
            s = s.substring(0, i+1);
        }

        return s;
    },

    Trim:function(str){
        return this.RTrim(this.LTrim(str));
    },

    inputError:function(elementErrorId){
        document.getElementById(elementErrorId).innerHTML = '<img src="/public/images/form/delete.png">';
    },

    inputSuccess:function(elementErrorId){
       document.getElementById(elementErrorId).innerHTML = '<img src="/public/images/form/tick-icon.png">';
    },

    checkInputValue:function(element){
        var elementValue = element.value;
        var elementErrorId = element.id+'_error';
        if(Common.Trim(elementValue) == ''){
            this.inputError(elementErrorId);
        }else{
            this.inputSuccess(elementErrorId);
        }
    },

    isEmail:function(elementValue){
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(elementValue)) {
            return false
        }else{
            return true;
        }
    },

    checkEmail:function(element) {
        var elementValue = element.value;
        var elementErrorId = element.id+'_error';
        if(this.Trim(elementValue) == ''){
            this.inputError(elementErrorId);
        }
        if(!this.isEmail(elementValue)){
            this.inputError(elementErrorId);
        }else{
            this.inputSuccess(elementErrorId);
        }
    },

    isNumber:function(myForm){
        
        if (/^((\d\.?)*)$/.test(myForm))
        {
            return true;
        }
        else
        {
            return false;
        }
    },

    checkPhoneNumber:function(element){
        var elementValue = element.value;
        var elementErrorId = element.id+'_error';
        if(this.isNumber(elementValue) == true){
            this.inputSuccess(elementErrorId);
        }else{
            this.inputError(elementErrorId);
        }
    },

    checkRetypePassword:function(pass1Input, pass2Input){
        var pass1Value = document.getElementById(pass1Input).value;
        var elementErrorId = pass2Input+'_error';
        var pass2Value = document.getElementById(pass2Input).value;
        if(pass1Value != pass2Value){
            this.inputError(elementErrorId)
        }else{
            this.inputSuccess(elementErrorId);
        }
        
    }
    

    

    

};
